package com.bobby.shop.service.impl;

import com.bobby.shop.mapper.ArticleMapper;
import com.bobby.shop.mapper.OrderItemMapper;
import com.bobby.shop.mapper.OrderMapper;
import com.bobby.shop.mapper.ShopCarMapper;
import com.bobby.shop.pojo.*;
import com.bobby.shop.service.OrderService;
import com.bobby.shop.vo.ArticleVO;
import com.bobby.shop.vo.OrderDetailVO;
import com.bobby.shop.vo.ShopCarVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.ch.SelectorImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Date 2020/12/25 17:55
 * @Created wenjunpei
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private ShopCarMapper shopCarMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void save(List<ShopCarVO> selectedShopCarList, User user) {
        Order order = new Order();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            order.setCreateDate(simpleDateFormat.parse(simpleDateFormat.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Double totalPrice = 0.0;
        for (ShopCarVO shopCarVO : selectedShopCarList) {
            Double price = shopCarVO.getPrice();
            Double discount = shopCarVO.getDiscount();
            Integer buyNum = shopCarVO.getBuyNum();
            totalPrice += price * buyNum * discount;
            Article article = articleMapper.selectByPrimaryKey(shopCarVO.getArticleId());
            article.setStorage(article.getStorage() - shopCarVO.getBuyNum());
            articleMapper.updateByPrimaryKey(article);
        }
        order.setUserId(user.getId());
        order.setAmount(totalPrice);
        simpleDateFormat = new SimpleDateFormat("yyyyHHddHHmmss");
        order.setOrderCode("OP-" + simpleDateFormat.format(new Date()));
        orderMapper.insert(order);
        for (ShopCarVO shopCarVO : selectedShopCarList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(order.getId());
            orderItem.setArticleId(shopCarVO.getArticleId());
            orderItem.setOrderNum(shopCarVO.getBuyNum());
            orderItemMapper.insert(orderItem);
            shopCarMapper.deleteByPrimaryKey(shopCarVO.getShopCarId());
        }
    }

    @Override
    public List<OrderDetailVO> findByUserId(Integer userId) {
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        List<OrderDetailVO> orderDetailVOList = new ArrayList<>();
        for (Order order : orderList) {
            OrderDetailVO orderDetailVO = new OrderDetailVO();
            BeanUtils.copyProperties(order,orderDetailVO);
            Integer orderId = order.getId();
            OrderItemExample orderItemExample = new OrderItemExample();
            OrderItemExample.Criteria criteria1 = orderItemExample.createCriteria();
            criteria1.andOrderIdEqualTo(orderId);
            List<OrderItem> orderItemList = orderItemMapper.selectByExample(orderItemExample);
            List<ArticleVO> articleVOList = new ArrayList<>();
            for (OrderItem orderItem : orderItemList) {
                ArticleVO articleVO = new ArticleVO();
                BeanUtils.copyProperties(orderItem,articleVO);
                Article article = articleMapper.selectByPrimaryKey(orderItem.getArticleId());
                BeanUtils.copyProperties(article,articleVO);
                articleVO.setArticleId(article.getId());
                articleVO.setBuyNum(orderItem.getOrderNum());
                articleVO.setOnlyOne(order.getId() + "_" +article.getId());
                articleVOList.add(articleVO);
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            orderDetailVO.setCreateDate(simpleDateFormat.format(order.getCreateDate()));
            if(order.getSendDate() != null) {
                orderDetailVO.setSendDate(simpleDateFormat.format(order.getSendDate()));
            }
            orderDetailVO.setOrderId(orderId);
            orderDetailVO.setArticleVOList(articleVOList);
            orderDetailVOList.add(orderDetailVO);
        }
        return orderDetailVOList;
    }
}
